package com.jiaotong.smart.smartjiaotong.controller.GreenWaveController;

import com.jiaotong.smart.smartjiaotong.entity.GreenWave.*;
import com.jiaotong.smart.smartjiaotong.entity.ResponseEntity;
import com.jiaotong.smart.smartjiaotong.service.GreenWaveService.ServiceImpl.GreenWaveServiceImpl;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.jdbc.Null;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


/**
 * @author 袁雨森
 * 绿波带相关controller
 */
@RestController
@RequestMapping("/greenwave")
public class GreenWaveController {

    @Resource
    GreenWaveServiceImpl greenWaveService;


    /**
     * 获取所有绿波带信息
     *
     * @return List<GreenWaveBand>
     */
    @GetMapping("/getAllGreenWave")
    public ResponseEntity getAllGreenWave() {

        ResAllGreenWave resAllGreenWave = greenWaveService.getAllGreenWave();
        if (resAllGreenWave == null) {
            return new ResponseEntity(500, "获取所有绿波带信息错误");
        } else {
            return new ResponseEntity(200, "获取所有绿波带信息成功", resAllGreenWave);
        }
    }

    /**
     * 获取所有正在使用的绿波带信息
     *
     * @return List<GreenWaveBand>
     */
    @GetMapping("/getAllUsingGreenWave")
    public ResponseEntity getAllUsingGreenWave() {

        ResAllGreenWave resAllGreenWave = greenWaveService.getAllUsingGreenWave();
        if (resAllGreenWave == null) {
            return new ResponseEntity(500, "获取已启用绿波带错误");
        } else {
            return new ResponseEntity(200, "获取已启用绿波带成功", resAllGreenWave);
        }
    }

    /**
     * 获取所有没有正在使用的绿波带信息
     *
     * @return List<GreenWaveBand>
     */
    @GetMapping("/getAllNotUsingGreenWave")
    public ResponseEntity getAllNotUsingGreenWave() {

        ResAllGreenWave resAllGreenWave = greenWaveService.getAllNotUsingGreenWave();
        if (resAllGreenWave == null) {
            return new ResponseEntity(500, "获取未启用绿波带错误");
        } else {
            return new ResponseEntity(200, "获取未启用绿波带成功", resAllGreenWave);
        }
    }


    /**
     * 更改绿波带启用状态
     */
    @PutMapping("/changeGreenWaveStatus/{id}")
    public ResponseEntity changeGreenWaveStatus(@PathVariable(value = "id") Long id,
                                                @RequestParam(value = "status") int status) {
        GreenWave greenWave = greenWaveService.getSingleGreenWave(id);
        if (status == greenWave.getStatus()) {
            if (status == 1) {
                return new ResponseEntity(200, "已处于使用状态");
            } else {
                return new ResponseEntity(200, "已处于未使用状态");
            }
        } else {
            int res = greenWaveService.updateGreenWaveStatus(id, status);
            if (res == 1) {
                return new ResponseEntity(200, "修改成功");
            } else {
                return new ResponseEntity(500, "修改失败");
            }
        }
    }

    // TODO: 2021/11/18 完善错误信息

    /**
     * 添加绿波带
     *
     * @param reqGreenWaveAndCross
     * @return
     */
    @PostMapping("/addGreenWave")
    public ResponseEntity addGreenWave(@RequestBody ReqGreenWaveAndCross reqGreenWaveAndCross) {
        greenWaveService.addGreenWave(reqGreenWaveAndCross);
        return new ResponseEntity(200, "添加成功");
    }

    /**
     * 获取绿波带详情
     *
     * @param greenWaveId
     * @return
     */
    @GetMapping("/{id}")
    public ResponseEntity getGreenWave(@PathVariable("id") Long greenWaveId) {
        ResGreenWaveAndCross resGreenWaveAndCross = greenWaveService.getGreenWaveDetail(greenWaveId);
        if (resGreenWaveAndCross != null) {
            return new ResponseEntity(200, "查询成功", resGreenWaveAndCross);
        } else {
            return new ResponseEntity(500, "查询失败");
        }
    }

    /**
     * 删除绿波带
     *
     * @param greenWaveId
     * @return
     */
    @DeleteMapping("/deleteGreenWave/{id}")
    public ResponseEntity deleteGreenWave(@PathVariable("id") Long greenWaveId) {
        int res = greenWaveService.deleteGreenWave(greenWaveId);
        if (res == 1) {
            return new ResponseEntity(200, "删除成功");
        } else {
            return new ResponseEntity(500, "删除失败");
        }
    }

    /**
     * 修改绿波带
     *
     * @param reqGreenWave
     * @return
     */
    @PutMapping("/updateGreenWave")
    public ResponseEntity updateGreenWave(@RequestBody GreenWave reqGreenWave) {
        int res = greenWaveService.updateGreenWave(reqGreenWave);
        if (res == 1) {
            return new ResponseEntity(200, "修改成功");
        } else {
            return new ResponseEntity(500, "修改失败");
        }
    }

    /**
     * 添加和修改绿波带内路口的信息
     *
     * @param greenWaveId
     * @param greenWaveCross
     * @return
     */
    @PostMapping("/addGreenWaveCross/{id}")
    public ResponseEntity addAndUpdateGreenWaveCross(@PathVariable(value = "id") Long greenWaveId, @RequestBody GreenWaveCross greenWaveCross) {
        greenWaveService.addAndUpdateGreenWaveCross(greenWaveId, greenWaveCross);
        return new ResponseEntity(200, "添加成功");
    }

    /**
     * 删除绿波带内的路口信息
     * @param greenWaveId
     * @param crossId
     * @return
     */
    @DeleteMapping("/deleteGreenWaveCross/{greenWaveId}")
    public ResponseEntity deleteGreenWaveCross(@PathVariable(value = "greenWaveId") Long greenWaveId, @RequestParam(value = "crossId") Long crossId) {
        int res = greenWaveService.deleteGreenWaveCross(greenWaveId, crossId);
        if (res == 1) {
            return new ResponseEntity(200, "删除成功");
        } else {
            return new ResponseEntity(500, "删除失败");
        }
    }
}